package server;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.eclipse.paho.client.mqttv3.*;


import java.util.*;

public class Connect {
    private static final String host = "tcp://47.108.254.101:1883";
    private static final String clientId = "server1";
    private static final String username = "jtzl";
    private static final String password = "jtzl@2023#!";
    private static final String topic = "+/gg_uuid/+/property/up";
    public static void main(String[] args) throws MqttException, InterruptedException {
        ExcelWriter writer = new ExcelWriter("D:\\mqtt数据\\sim去重2小时.xlsx");
        MqttClient client = new MqttClient(host, clientId);
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setKeepAliveInterval(180);
        connOpts.setUserName(username);
        connOpts.setPassword(password.toCharArray());
        connOpts.setCleanSession(true);
        List<Map<String,Object>> data=new ArrayList<>();
        Set<String> set=new HashSet<>();
        client.setCallback(new MqttCallback(){
            @Override
            public void messageArrived(String topic, MqttMessage message) throws Exception {

                String msg =new String(message.getPayload());
                JSONObject jsonObject = JSONUtil.parseObj(msg);
                String params=jsonObject.getStr("params");
                JSONObject obj=JSONUtil.parseObj(params);
                String sim=obj.getStr("sim_ccid");
                if(StrUtil.isNotEmpty(sim)&&!set.contains(sim)){
                    set.add(sim);
                    System.out.println("sim_ccid:"+obj.getStr("sim_ccid"));
                    Map<String,Object> map=TopicHandle(topic);
                    map.put("sim_ccid",obj.getStr("sim_ccid"));
                    data.add(map);
                }


            }
            @Override
            public void connectionLost(Throwable cause) {
                System.out.println("连接断开，原因：" + cause.getMessage());
            }
            @Override
            public void deliveryComplete(IMqttDeliveryToken token) {

            }
        });
        //建立连接
        client.connect(connOpts);
        //订阅主题
        client.subscribe(topic);
        Timer timer = new Timer();
        //定时停止
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    client.disconnect();
                    writer.write(data);
                    writer.close();
                } catch (MqttException e) {
                    e.printStackTrace();
                }
            }
        }, 60 * 1000 * 60*2);


    }
    public static Map<String,Object> TopicHandle(String topic){
        String[] topicArr=topic.split("/");
        Map<String,Object> map=new HashMap<>();
        map.put("项目编码",topicArr[0]);
        map.put("设备编码",topicArr[1]);
        map.put("mac",topicArr[2]);
        return map;
    }

}
